package cn.lds.j375.healthexam.interceptor;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Objects;

@Component
public class LoginInterception implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        HttpSession session = request.getSession();

        Object loginUser = session.getAttribute("loginUser");

        if (Objects.isNull(loginUser)){
            request.setAttribute("err","请登录后访问");
            request.getRequestDispatcher("/user/login").forward(request,response);
            return false;
        }
        return true;
    }
}
